package com.intelligent.health.quartz.controller;

import com.intelligent.health.quartz.config.QuartzManager;
import entity.R;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/04/08/1:02
 * @Description: 定时任务控制器
 */
@Slf4j
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private QuartzManager quartzManager;

    /**
     * 任务调度执行
     *
     * @param job   job的名称
     * @param group job的组
     * @param cron  cron表达式
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/start")
    public R start(@RequestParam("job") String job,
                   @RequestParam("group") String group,
                   @RequestParam("cron") String cron) throws SchedulerException {

        quartzManager.startJob(job, group, cron);
        String jobInfo = quartzManager.getjobInfo(job, group);
        log.info("执行任务调度: {}", jobInfo);
        return R.ok();
    }


    /**
     * 暂停调度任务
     *
     * @param job
     * @param group
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/pause")
    public R pause(@RequestParam("job") String job,
                   @RequestParam("group") String group) throws SchedulerException {

        quartzManager.pauseJob(job, group);
        String jobInfo = quartzManager.getjobInfo(job, group);
        log.info("暂停任务调度: {}", jobInfo);
        return R.ok();
    }

    /**
     * 恢复调度任务
     *
     * @param job
     * @param group
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/resume")
    public R resume(@RequestParam("job") String job,
                    @RequestParam("group") String group) throws SchedulerException {

        quartzManager.resumeJob(job, group);
        String jobInfo = quartzManager.getjobInfo(job, group);
        log.info("恢复任务调度: {}", jobInfo);
        return R.ok();
    }

    /**
     * 删除调度任务
     *
     * @param job
     * @param group
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/delete")
    public R delete(@RequestParam("job") String job,
                    @RequestParam("group") String group) throws SchedulerException {

        quartzManager.deleteJob(job, group);
        String jobInfo = quartzManager.getjobInfo(job, group);
        log.info("删除任务调度: {}", jobInfo);
        return R.ok();
    }

}
